#include "glad/glad.h"
#include "GLFW/glfw3.h"
#include "common/shader.hpp"
#include "common/stb_image.hpp"

#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/type_ptr.hpp"

#include <iostream>

void framebuffer_size_callback(GLFWwindow* win, int32_t width, int32_t height) {
	glViewport(0, 0, width, height);
}

void handle_event(GLFWwindow* win) {
	if (glfwGetKey(win, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
		glfwSetWindowShouldClose(win, true);
	}
}

int main() {
	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

#ifdef __apple__
	glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif

	GLFWwindow* win = glfwCreateWindow(800, 600, "OpenGL", nullptr, nullptr);
	if (!win) {
		std::cout << "failed to create opengl window." << std::endl;
		glfwTerminate();
		return -1;
	}

	glfwMakeContextCurrent(win);
	glfwSetFramebufferSizeCallback(win, framebuffer_size_callback);
	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
	{
		std::cout << "Failed to initialize GLAD" << std::endl;
		return -1;
	}

	glm::vec4 vec(1.0f, 0.0f, 0.0f, 1.0f);
	glm::mat4 trans = glm::mat4(1.0f);
	trans = glm::translate(trans, glm::vec3(1.0f, 1.0f, 0.0f));
	vec = trans * vec;
	std::cout << vec.x << vec.y << vec.z << std::endl;

	Shader shader(R"(E:\ws\learn-opengl\hello-texture\shader.vs)", R"(E:\ws\learn-opengl\hello-texture\shader.fs)");

	float vertices[] = {
		 0.5f,  0.5f,   0.0f, 1.0f, 0.0f, 0.0f,  1.0f, 1.0f,
		 0.5f, -0.5f,   0.0f, 0.0f, 1.0f, 0.0f,  1.0f, 0.0f,
		-0.5f, -0.5f,   0.0f, 0.0f, 0.0f, 1.0f,  0.0f, 0.0f,
		-0.5f,  0.5f,   0.0f, 1.0f, 0.0f, 1.0f,  0.0f, 1.0f,
	};

	uint32_t indices[] = {
		0, 1, 3,
		1, 2, 3
	};

	uint32_t VBO, VAO;
	glGenVertexArrays(1, &VAO);
	glGenBuffers(1, &VBO);
	glBindVertexArray(VAO);

	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

	uint32_t EBO;
	glGenBuffers(1, &EBO);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
	glEnableVertexAttribArray(1);
	glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
	glEnableVertexAttribArray(2);

	uint32_t image_texture;
	glGenTextures(1, &image_texture);
	glBindTexture(GL_TEXTURE_2D, image_texture);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	int image_width, image_height, nr_channels;
	unsigned char* image_data = stbi_load(R"(E:\ws\learn-opengl\asset\container.jpg)", &image_width, &image_height, &nr_channels, 0);
	if (!image_data) {
		std::cerr << "failed load image wall.jpg." << std::endl;
		return -1;
	}
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image_width, image_height, 0, GL_RGB, GL_UNSIGNED_BYTE, image_data);
	glGenerateMipmap(GL_TEXTURE_2D);
	stbi_image_free(image_data);

	uint32_t image_texture2;
	glGenTextures(1, &image_texture2);
	glBindTexture(GL_TEXTURE_2D, image_texture2);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	stbi_set_flip_vertically_on_load(true);
	image_data = stbi_load(R"(E:\ws\learn-opengl\asset\awesomeface.png)", &image_width, &image_height, &nr_channels, 0);
	if (!image_data) {
		std::cerr << "failed load image awesomeface.png." << std::endl;
		return -1;
	}
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image_width, image_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data);
	glGenerateMipmap(GL_TEXTURE_2D);
	stbi_image_free(image_data);

	// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

    shader.use();
	shader.set_int("OurTexture1", 0);
	shader.set_int("OurTexture2", 1);
	while (!glfwWindowShouldClose(win)) {
		handle_event(win);

		glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);

		//float time = glfwGetTime();
		//float green_value = sin(time) / 2.0f + 0.5f;
		//int32_t vertex_color_location = glGetUniformLocation(shader_program, "OutColor");
		//glUniform4f(vertex_color_location, 0.0f, green_value, 0.0f, 0.1f);

		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D, image_texture);
		glActiveTexture(GL_TEXTURE1);
		glBindTexture(GL_TEXTURE_2D, image_texture2);

		glBindVertexArray(VAO);
		// glDrawArrays(GL_TRIANGLES, 0, 3);
		glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

		glfwSwapBuffers(win);
		glfwPollEvents();
	}

	glDeleteVertexArrays(1, &VAO);
	glDeleteBuffers(1, &VBO);
	glDeleteBuffers(1, &EBO);

	glfwTerminate();

	return 0;
}

